
/* BrailleBlaster Braille Transcription Application
  *
  * Copyright (C) 2010, 2012
  * ViewPlus Technologies, Inc. www.viewplus.com
  * and
  * Abilitiessoft, Inc. www.abilitiessoft.com
  * and
  * American Printing House for the Blind, Inc. www.aph.org
  *
  * All rights reserved
  *
  * This file may contain code borrowed from files produced by various 
  * Java development teams. These are gratefully acknoledged.
  *
  * This file is free software; you can redistribute it and/or modify it
  * under the terms of the Apache 2.0 License, as given at
  * http://www.apache.org/licenses/
  *
  * This file is distributed in the hope that it will be useful, but
  * WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE
  * See the Apache 2.0 License for more details.
  *
  * You should have received a copy of the Apache 2.0 License along with 
  * this program; see the file LICENSE.
  * If not, see
  * http://www.apache.org/licenses/
  *
  * Maintained by John J. Boyer john.boyer@abilitiessoft.com
*/

package org.brailleblaster.importers;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

import org.w3c.dom.Document;
import org.xml.sax.SAXException;

public class XSLtransformer {
	
	public XSLtransformer () {
		
	}

	public boolean transformXml (String xsl, String fileName, String outFile) {

		Document document;
		
		try {
			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
			/* turn off DTD validation */
			factory.setValidating(false);
			factory.setFeature("http://xml.org/sax/features/namespaces", false);
			factory.setFeature("http://xml.org/sax/features/validation", false);
			factory.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
			factory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);

			DocumentBuilder builder = factory.newDocumentBuilder();

			document = builder.parse(new FileInputStream(fileName));
		
			File out = new File(outFile);
		StreamSource stylesource = new StreamSource(new File (xsl));
		TransformerFactory tFactory = TransformerFactory.newInstance();
		Transformer transformer = tFactory.newTransformer(stylesource);
		
		DOMSource source = new DOMSource(document);
        StreamResult result = new StreamResult(out);

        transformer.transform(source, result);
        
		} catch (TransformerConfigurationException tce) {
            // Error generated by the parser
            System.err.println("\n** Transformer Factory error");
            System.err.println("   " + tce.getMessage());

            // Use the contained exception, if any
            Throwable x = tce;

            if (tce.getException() != null) {
                x = tce.getException();
                x.printStackTrace();
            }

            return false;
            
        } catch (TransformerException te) {
            // Error generated by the parser
            System.out.println("\n** Transformation error");
            System.out.println("   " + te.getMessage());

            // Use the contained exception, if any
            Throwable x = te;

            if (te.getException() != null) {
                x = te.getException();
                x.printStackTrace();
            }

            return false;
            
        } catch (SAXException sxe) {
            // Error generated by this application
            // (or a parser-initialization error)
            Exception x = sxe;

            if (sxe.getException() != null) {
                x = sxe.getException();
                x.printStackTrace();
            }

            System.out.println("\n** Transformation error");
            System.out.println("   " + sxe.getMessage());

            return false;
            
        } catch (ParserConfigurationException pce) {
            // Parser with specified options can't be built
            System.out.println("\n** Transformation error");
            System.out.println("   " + pce.getMessage());

        	//pce.printStackTrace();
            return false;

        } catch (IOException ioe) {
            // I/O error
            System.out.println("\n** Transformation error");
            System.out.println("   " + ioe.getMessage());

            //ioe.printStackTrace();
            return false;
        }

		return true;
	}
}
